package com.wan37.logic.utils;

import com.wan37.protobuf.constant.CommonBase;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @author : luoyong
 * @date : 2020-06-23 14:55
 **/
@Component
public class BuffUtil {

    private Set<String> addBuff = new HashSet<>();

    private Set<String> multiplyBuff = new HashSet<>();

    private Map<String, String> addBuffOrm = new HashMap<>();

    private Map<String, String> multiplyBuffOrm = new HashMap<>();

    public BuffUtil() throws IllegalAccessException {
        addBuffOrm.put(CommonBase.BUFF_ADD_ATN, CommonBase.ATN);
        addBuffOrm.put(CommonBase.BUFF_ADD_INT, CommonBase.INT);
        addBuffOrm.put(CommonBase.BUFF_ADD_DEF, CommonBase.DEF);
        addBuffOrm.put(CommonBase.BUFF_ADD_RES, CommonBase.RES);

        multiplyBuffOrm.put(CommonBase.BUFF_ATN, CommonBase.ATN);
        multiplyBuffOrm.put(CommonBase.BUFF_INT, CommonBase.INT);
        multiplyBuffOrm.put(CommonBase.BUFF_DEF, CommonBase.DEF);
        multiplyBuffOrm.put(CommonBase.BUFF_RES, CommonBase.RES);
        Class clazz = CommonBase.class;
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            String name = field.getName();
            if ("BU".equals(name.substring(0, 2))) {
                if ("ADD".equals(name.split("_")[1])) {
                    addBuff.add(field.get(null).toString());
                } else {
                    multiplyBuff.add(field.get(null).toString());
                }
            }
        }
    }

    public Long add(Long a, Long b) {
        return a + b;
    }

    public Long multiply(Long a, Long b) {
        return a * b;
    }

    public Long reduce(Long a, Long b) {
        return a - b;
    }

    public Long divide(Long a, Long b) {
        return a / b;
    }

    public Set<String> getAddBuff() {
        return addBuff;
    }


    public Set<String> getMultiplyBuff() {
        return multiplyBuff;
    }

    /**
     * 角色数值增加的操作方法
     *
     * @param numberMap
     * @param roleNumberMap
     */
    public void positive(Map<String, Long> numberMap, Map<String, Long> roleNumberMap) {
        numberMap.keySet().forEach(key -> {
            if (getAddBuff().contains(key)) {
                roleNumberMap.put(addBuffOrm.get(key), add(roleNumberMap.get(addBuffOrm.get(key)), numberMap.get(key)));
            }
            if (getMultiplyBuff().contains(key)) {
                roleNumberMap.put(multiplyBuffOrm.get(key), multiply(roleNumberMap.get(multiplyBuffOrm.get(key)), numberMap.get(key)));
            }
        });
    }

    /**
     * 角色数值减少的操作方法
     *
     * @param numberMap
     * @param roleNumberMap
     */
    public void negative(Map<String, Long> numberMap, Map<String, Long> roleNumberMap) {
        numberMap.keySet().forEach(key -> {
            if (getAddBuff().contains(key)) {
                roleNumberMap.put(addBuffOrm.get(key), reduce(roleNumberMap.get(addBuffOrm.get(key)), numberMap.get(key)));
            }
            if (getMultiplyBuff().contains(key)) {
                roleNumberMap.put(multiplyBuffOrm.get(key), divide(roleNumberMap.get(multiplyBuffOrm.get(key)), numberMap.get(key)));
            }
        });
    }

}
